package org.benetech.exception;

/**
 * An exception which needs to be made unchecked and handled further up the
 * stack.
 * @author Reuben Firmin
 */
public class WrappedException extends RuntimeException implements HasErrorCode, HasDetailedDescription {

    private ErrorCode errorCode;

    private String detailedDescription;

    /**
     * Construct with the exception being wrapped.
     * @param e Never null
     */
    public WrappedException(final Exception e)
    {
        super(e);
        this.setStackTrace(e.getStackTrace());
        if (e instanceof HasErrorCode) {
            this.errorCode = ((HasErrorCode) e).getErrorCode();
        }
        if (e instanceof HasDetailedDescription) {
            this.detailedDescription = ((HasDetailedDescription) e).getDetailedDescription();
        }
    }

    /**
     * @param e Null not allowed.
     * @param errorCode Null allowed.
     * @param detailedDescription Null allowed.
     */
    public WrappedException(final Exception e, final ErrorCode errorCode, final String detailedDescription)
    {
        this(e);
        // we only override these values if the incoming params are non-null.
        this.errorCode = (errorCode == null) ? this.errorCode : errorCode;
        this.detailedDescription = (detailedDescription == null) ? this.detailedDescription : detailedDescription;
    }

    /**
     * {@inheritDoc}
     */
    public final String getDetailedDescription()
    {
        return this.detailedDescription;
    }

    /**
     * {@inheritDoc}
     */
    public final ErrorCode getErrorCode()
    {
        return this.errorCode;
    }

}
